import { createRouter, createWebHashHistory } from "vue-router";
import { getStorage } from "@/utils/localStorage";
import { showFailToast, showLoadingToast } from "vant";



let router = createRouter({
    history: createWebHashHistory(process.env.BASE_URL),
    routes: [
        // 基本路由跳转 --- 主页、搜索、AI聊天、设置
        {
            path: "/",
            component: () => import("@/views/VideoCall/MainEntranceView.vue"),
            children: [
                {
                    path: "/",
                    redirect: "/home-page"
                },
                {
                    path: "/forum",
                    name: 'forum',
                    component: () => import("@/views/Base/ForumView.vue"),
                },
                {
                    path: "home-page",
                    name: 'homePage',
                    component: () => import("@/views/Base/HomePage.vue"),
                },
                {
                    path: "search-view",
                    name: 'searchView',
                    component: () => import("@/views/Base/HotNewsView.vue")
                },
                {
                    path: "chat-view",
                    name: 'chatView',
                    component: () => import("@/views/Base/AIChat.vue")
                },
                {
                    path: "setting-view",
                    name: 'settingView',
                    component: () => import("@/views/Base/SettingsView.vue")
                },
            ]
        },

        // 用户相关跳转 --- 登录、个人详细信息、修改密码, 反馈
        {
            path: '/user',
            children: [
                {
                    path: 'login/:types?',
                    name: 'UserLogin',
                    component: () => import('@/views/user/LoginView.vue')
                },
                {
                    path: 'register/:types?',
                    name: 'UserRegister',
                    component: () => import('@/views/user/RegisterView.vue')
                },
                {
                    path: "show-my-posts",
                    name: 'showMyPosts',
                    component: () => import("@/views/user/ShowMyPosts.vue")
                },
                {
                    path: "community-course",
                    name: 'communityCourse',
                    component: () => import("@/views/user/CommunityCourse.vue")
                },
                {
                    path: "post-detail/:postId",
                    name: 'postDetail',
                    component: () => import("@/views/user/PostDetail.vue")
                },
                {
                    path: "my-details",
                    name: 'UserDetails',
                    component: () => import("@/views/SettingsPage/MyDetails.vue")
                },
                {
                    path: "change-password",
                    name: 'UserChangePassword',
                    component: () => import("@/views/SettingsPage/changePassword.vue")
                },
                {
                    path: "feedback",
                    name: 'UserFeedback',
                    component: () => import("@/views/SettingsPage/FeedbackView.vue")
                },
            ]
        },

        // 接打视频相关页面
        {
            path: "/video-call-calling-view",
            component: () => import("@/views/VideoCall/VideoCallCallingView.vue")
        },
        {
            path: "/video-call-answer-view",
            component: () => import("@/views/VideoCall/VideoCallAnswerView.vue")
        },

        // 学习如何接听视频电话
        {
            path: '/learn-call-phone',
            name: 'learnCallPhone',
            component: () => import('@/views/user/LearnCallPhone.vue')
        },
        // 全都匹配不到时，跳转到主页
        {
            path: "/:catchAll(.*)",
            component: () => import("@/views/Error/NotFound.vue")
        }
    ]
});

// 需要登录才能访问的页面
const authUrls = [
    '/user/my-details',
    '/user/change-password',
    '/friends-list',
    '/setting-view',
    '/chat-view',
    '/video-call-calling-view',
    '/video-call-answer-view',
    '/forum'
]

router.beforeEach((to, from, next) => {
    // 加载提示
    from.meta.loadingToast = showLoadingToast({
        duration: 0,
        forbidClick: true,
        loadingType: "spinner",
        message: "加载中..."
    });

    if (!authUrls.includes(to.path)) {
        next()
        return
    }

    if (!getStorage('userStore')?.userInfo.isLogin) {
        from.meta.loadingToast?.close()

        showFailToast({
            duration: 1000,
            forbidClick: true,
            message: '请先登录'
        })
        next('/user/login')
    } else {
        next()
    }
    from.meta.loadingToast?.close()
});

router.afterEach((to, from) => {
    //当页面被缓存时，有可能from.meta.loadingToast为空，所以用可选链操作符
    from.meta.loadingToast?.close()
});

export default router;